#!/usr/bin/env bash
containerName="jenkins-blueocean"
echo '>>> Please later ... ...'
set -e
sudo docker pull  jenkinsci/blueocean:latest | 
   grep "Image is up to date" ||
   (echo Already up to date. Exiting... && exit 0)


echo '>>> Get old container id'
CID=$(sudo docker ps --all | grep  ${containerName} | awk '{print $1}')
echo $CID


echo ">>> Stopping and deleting old container  ${containerName}"
if [ "$CID" != "" ];
then
  sudo docker stop $CID
  sudo docker rm $CID
fi
echo ">>> Starting new container  ${containerName}"


sudo docker run  --name  ${containerName} -u root  -d -p 8000:8080  -p 50000:50000 -v /docker/jenkins-data:/var/jenkins_home  -v /var/run/docker.sock:/var/run/docker.sock -v /etc/localtime:/etc/localtime  -e JAVA_OPTS="-Xms128m -Xmx256m -XX:MaxNewSize=256m -Duser.timezone=Asia/Shanghai -Dsun.jnu.encoding=UTF-8 -Dfile.encoding=UTF-8"  -e LANG=en_US.UTF-8 --restart=always jenkinsci/blueocean:latest
#插件下载超时更改配置：
sed -i 's/http:\/\/updates.jenkins-ci.org\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g'  /docker/jenkins-data/updates/default.json && sed -i 's/http:\/\/www.google.com/https:\/\/www.baidu.com/g' /docker/jenkins-data/updates/default.json